package com.itao.service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.itao.dao.AccountDao;
import com.itao.model.Account;
import com.itao.model.SecUser;
import com.itao.utils.MD5;

/**
 * @ClassName: OpcnetUserDetailsService
 * @Description:
 */

public class OpcnetUserDetailsService implements UserDetailsService {

	@Resource
	private AccountDao accountDao;
	
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
		SimpleGrantedAuthority au = new SimpleGrantedAuthority("ROLE_USER");
		list.add(au);
		Account account = accountDao.findByCol("account", username);
		//account.setPassword(MD5.md5(account.getPassword()+"{"+account.getName()+"}"));
		UserDetails user = new SecUser(account,list);
		return user;
	}

}
